/******************************************************************************
 * This program is a 100% Java Email Server.
 ******************************************************************************
 * Copyright (c) 2001-2011, Eric Daugherty (http://www.ericdaugherty.com)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the copyright holder nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 * For current versions and more information, please visit:
 * http://javaemailserver.sf.net/
 *
 * or contact the authors at:
 * java@ericdaugherty.com
 * andreaskyrmegalos@hotmail.com
 *
 ******************************************************************************
 * This program is based on the CSRMail project written by Calvin Smith.
 * http://crsemail.sourceforge.net/
 ******************************************************************************
 *
 * $Rev$
 * $Date$
 *
 ******************************************************************************/

package com.ericdaugherty.mail;

//Java Imports
import java.io.*;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

//Local Imports
import com.ericdaugherty.mail.server.utils.FileUtils;

/**
 *
 * @author Andreas Kyrmegalos
 */
public class Utils {

    public static void copyFiles(File directory, File target) throws IOException{
       File[] allTestFiles = directory.listFiles();
       for (int i=0;i<allTestFiles.length;i++) {
          if(allTestFiles[i].isDirectory()) {
             String newSubDir = allTestFiles[i].getPath();
             newSubDir = newSubDir.substring(newSubDir.lastIndexOf(File.separator)+1);
             File targetDir = new File(target, newSubDir);
             targetDir.mkdir();
             copyFiles(allTestFiles[i], targetDir);
          }
          else {
             FileUtils.copyFile(allTestFiles[i], new File(target, allTestFiles[i].getName()));
          }
       }
    }

    public static void deleteFiles(File directory) throws IOException{
       File[] allTestFiles = directory.listFiles();
       for (int i=0;i<allTestFiles.length;i++) {
          if(allTestFiles[i].isDirectory()) {
             deleteFiles(allTestFiles[i]);
             if (!allTestFiles[i].delete()) {
                System.out.println("unable to delete " +allTestFiles[i]);
             }
          }
          else {
             if (!allTestFiles[i].delete()) {
                System.out.println("unable to delete " +allTestFiles[i]);
             }
          }
       }
    }

   public static byte[] getOriginalMD5(File input) throws IOException {

      InputStream is = new FileInputStream(input);
      byte[] result = new byte[16];
      int count = 0;
      try {
         int nextByte;
         for (;count<16;) {
            nextByte = is.read();
            result[count++] = (byte)(nextByte&0xff);
         }
         return result;
      }
      finally {
         try {
            is.close();
         }
         catch (IOException ioe) {}
      }
   }

   public static byte[] getDerivedMD5(File input) throws IOException, GeneralSecurityException{
      InputStream is = new FileInputStream(input);
      MessageDigest md = MessageDigest.getInstance("MD5");
      try {
         int nextByte;
outer:   while((nextByte=is.read())!=-1) {
            if (nextByte==0x0a||nextByte==0x0d) continue;
            for (int i=0;i<initialField.length;i++) {
               if (nextByte!=initialField[i]) continue outer;
               if ((nextByte=is.read())==-1) {
                  throw new IOException("Reached end of file before discovering initial field");
               }
            }
            for (int i=0;i<initialField.length;i++) {
               md.update((byte)(initialField[i]&0xff));
            }
            break;
         }
         while((nextByte=is.read())!=-1) {
            if (nextByte==0x0a||nextByte==0x0d) continue;
            md.update((byte)(nextByte&0xff));
         }
         return md.digest();
      }
      finally {
         try {
            is.close();
         }
         catch (IOException ioe) {}
      }
   }

   private static final byte[] initialField = "X-Priority: Normal".getBytes();
}
